<?php
/**
 * ModulesParam Model
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single
 * modules_param entry
 *
 * @version $Id: ModulesParam.php 73 2010-04-25 00:30:44Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Default_Model_ModulesParamMapper
 * @package default
 * @subpackage Model
 */
class Default_Model_ModulesParam extends Default_Model_Doctrine_BaseModulesParam
{

    public function getParamByResourceId($resource_id)
    {

        $db = $this->getTable();
        $res = $db->findBy('resource_id',$resource_id)->toArray();

        $arr = array();
        $translate = Zend_Registry::get('Zend_Translate');
        // 預設值
        $module_default = APPLICATION_PATH.'/modules/'.$resource_id.'/languages/'.$translate->getLocale().'/'.
		'module-cfg-default.php';
        if (is_file($module_default)) {
            $arr = include(realpath($module_default));
        }
        if ($res) {
            foreach($res as $val)
            $arr[$val['param']] = $val['value'];
        }

        return $arr;
    }

    /**
     * 參數調整
     * @param unknown_type $resource_id
     * @return unknown_type
     */
    function getParamForm($resource_id)
    {
        $translate = Zend_Registry::get('Zend_Translate');
        $xml_file = APPLICATION_PATH.'/modules/'.$resource_id.'/module.xml';
        if (is_file($xml_file)) {
            $xml = new Zend_Config_Xml($xml_file);
            //      $this->view->xml = $xml;
            $form = new Sfs_Form($xml->param);
            $form->setAttrib('id','paramForm');
            $form->addElement('submit','PARAM_SAVE',array(
                        'label'=>'save'
                        ))
                        ->addElement('hidden','PARAM_RESOURCE_ID',array(
                       'required'=>true,
                        ));
                        $form->getElement('PARAM_RESOURCE_ID')->setValue($resource_id);
                        // 預設值
                        $module_default = APPLICATION_PATH.'/modules/'.$resource_id.'/languages/'.$translate->getLocale().'/'.
				'module-cfg-default.php';
                        $module_default_arr = array();
                        if (is_file($module_default)) {
                            $module_default_arr = include(realpath($module_default));
                            $form->setDefaults($module_default_arr);
                        }
                        // 已在資料回復

                        $db = $this->getTable();
                        $res = $db->findBy('resource_id',$resource_id)->toArray();
                        if ($res) {
                            $new_param = array();
                            foreach($res as $val) {
                                $form->setDefault($val['param'],$val['value']);
                                $new_param[]= $val['param'];
                            }
                            $module_default_arr_index = array_keys($xml->param->elements->toarray());

                            // $this->view->new_param = Zend_Json::encode(array_diff($module_default_arr_index,$new_param));

                        }

                        //              $this->view->paramform = $form;


                        $tr_name = APPLICATION_PATH.'/modules/'.$resource_id.'/languages/'.$translate->getLocale().'/'.
				'module-cfg.php';
                        if (is_file($tr_name)){
                            $translate->addTranslation($tr_name);
                        }
                        return $form;
        }
        return false;
    }

    /**
     * 儲存參數
     * @return unknown_type
     */
    public function saveParam(Zend_Controller_Request_Abstract $request)
    {
        // $request = $this->getRequest();
        $resourseId = $request->getParam('PARAM_RESOURCE_ID');
        if ($request->isPost()) {
            $form = $this->getparamForm($resourseId);
            if ($form->isValid($request->getParams())){
                //$db= Zend_Db_Table::getDefaultAdapter();
                foreach($form->getValues() as $id=>$val) {
                    if ($id=='PARAM_RESOURCE_ID' or $id=='PARAM_SAVE')
                    continue;
                    unset($arr);
                    $q = Doctrine_Query::create()
                    ->from("Default_Model_ModulesParam a")
                    ->where('a.resource_id=? AND a.param=?',array($resourseId, $id));
                    $record = $q->execute();
                    if ($record->count() == 0) {
                        $this->resource_id = $resourseId;
                        $this->param = $id;
                        $this->value = $val;
                        $this->save();
                    } else {
                        $q = Doctrine_Query::create()
                        ->update('Default_Model_ModulesParam')
                        ->set('value','?',$val)
                        ->where('resource_id=? AND param=?',array($resourseId, $id));
                        $q->execute();
                    }

                }
                return array('success'=>1);
            }
            else
            return (array('error'=>$form->getMessages()));
        }
        return(array('error'=>'error'));
    }


}
